/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.huawei.hmtagview.slice;

import com.cunoraz.tagview.DeConstants;
import com.cunoraz.tagview.Tag;
import com.cunoraz.tagview.TagView;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.huawei.hmtagview.Models.TagClass;
import com.huawei.hmtagview.ResourceTable;
import com.huawei.hmtagview.util.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;

import java.util.ArrayList;

/**
 * 入口类
 *
 * @author jiaojunying
 * @since 2021-03-22
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final float RADIUS = 10f;

    private static final int HARFVALUE = 2;

    private TagView tagGroup;

    private TextField editText;

    private ArrayList<TagClass> tagList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }

    private void initViewFirst() {
        tagGroup = (TagView) findComponentById(ResourceTable.Id_tag_group);
        editText = (TextField) findComponentById(ResourceTable.Id_editText);
        prepareTags();
        editText.addTextObserver(new TextField.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                setTags(s);
            }
        });
        tagGroup.setOnTagLongClickListener(new TagView.OnTagLongClickListener() {
            @Override
            public void onTagLongClick(Tag tag, int position) {
                ToastUtil.toastShowLong(getContext(), "Long Click: " + tag.getText(), ToastUtil.TOAST_SHORT_TIME);
            }
        });
    }

    private void initView() {
        initViewFirst();
        tagGroup.setOnTagClickListener(new TagView.OnTagClickListener() {
            @Override
            public void onTagClick(Tag tag, int position) {
                editText.setText(tag.getText());
            }
        });

        tagGroup.setOnTagDeleteListener(new TagView.OnTagDeleteListener() {
            @Override
            public void onTagDeleted(final TagView view, final Tag tag, final int position) {
                CommonDialog dialog = new CommonDialog(getContext());
                dialog.setAutoClosable(true);
                dialog.setAlignment(LayoutAlignment.CENTER);
                Component component = LayoutScatter.getInstance(MainAbilitySlice.this).
                        parse(ResourceTable.Layout_delete_dialog, null, false);
                dialog.setContentCustomComponent(component);
                Text deleText = (Text) component.findComponentById(ResourceTable.Id_text_dialog);
                deleText.setText("\"" + tag.getText() + "\" will be delete. Are you sure?");
                Button yesButton = (Button)component.findComponentById(ResourceTable.Id_yes_button);
                yesButton.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        view.remove(position);
                        ToastUtil.toastShowLong(getContext(), "\"" + tag.getText()
                                + "\" deleted", ToastUtil.TOAST_SHORT_TIME);
                        dialog.destroy();
                    }
                });
                Button noButton = (Button)component.findComponentById(ResourceTable.Id_no_button);
                noButton.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        dialog.destroy();
                    }
                });
                dialog.show();
            }
        });
    }

    private void prepareTags() {
        tagList = new ArrayList<>();
        JsonParser parser = new JsonParser();
        JsonArray jsonArray = parser.parse(DeConstants.COUNTRIES).getAsJsonArray();
        Gson gson = new Gson();
        for (JsonElement tag : jsonArray) {
            TagClass tagClass = gson.fromJson(tag, TagClass.class);
            tagList.add(new TagClass(tagClass.getSinif(), tagClass.getName()));
        }
    }

    private void setTags(CharSequence cs) {
        /**
         * for empty edittext
         */
        if (cs.toString().equals("")) {
            tagGroup.addTags(new ArrayList<Tag>());
            return;
        }

        String text = cs.toString();
        ArrayList<Tag> tags = new ArrayList<>();
        Tag tag;
        for (int index = 0; index < tagList.size(); index++) {
            if (tagList.get(index).getName().toLowerCase().startsWith(text.toLowerCase())) {
                tag = new Tag(tagList.get(index).getName());
                tag.setRadius(RADIUS);
                tag.setLayoutColor(Color.getIntColor(tagList.get(index).getColor()));
                if (index % HARFVALUE == 0) {
                    tag.setDeletable(true);
                }
                tags.add(tag);
            }
        }
        tagGroup.addTags(tags);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
